﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ELGGAPI.Models;
using ELGGAPI.Data;

namespace ELGGAPI.Controllers
{
    public class GameInstanceDataController : ApiController
    {
        public Response<Idclass> PostManageGameInstanceData(Request<GameInstanceDataModels> request)
        {
            var response = new Response<Idclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    string strInstanceGUID = GameInstanceDataDomainModel.AddUpdateGameInstanceData(request.Data);
                    if (strInstanceGUID == "-100")
                    {
                        response.Message = "Invalid id";
                        return response;
                    }
                    else
                    {
                        response.Data = new Idclass { id = int.Parse("0"+strInstanceGUID) };
                    }

                }

                return (response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }

        public Response<GameInstanceGraphModels> GetGameInstanceGraph(string gameid,
            string month,string year,  string access_token)
        {
            var response = new Response<GameInstanceGraphModels>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    int intgameid = 0;
                    if (!string.IsNullOrEmpty(gameid))
                    {
                        intgameid = int.Parse("0" + gameid);
                    }

                    int intmonth = 0;
                    if (!string.IsNullOrEmpty(month))
                    {
                        intmonth = int.Parse("0" + month);
                    }

                    int intyear = 0;
                    if (!string.IsNullOrEmpty(year))
                    {
                        intyear = int.Parse("0" + year);
                    }
                    response.Data = GameInstanceGraphDomain.GetGameInstanceGraph(user.id, intgameid,intmonth,intyear);
                }
                return (response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<IEnumerable<GameInstanceDataModels>> GetGameInstanceData(string gameid, string palyerid, string access_token)
        {
            var response = new Response<IEnumerable<GameInstanceDataModels>>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    
                    int intplayerid = 0;
                    if (!string.IsNullOrEmpty(palyerid))
                    {
                        intplayerid = int.Parse("0" + palyerid);
                    }

                    int intgameid = 0;
                    if (!string.IsNullOrEmpty(gameid))
                    {
                        intgameid = int.Parse("0" + gameid);
                    }
                    response.Data = GameInstanceDataDomainModel.GetGameInstanceDataList(user.id, intplayerid, intgameid);

                }

                return (response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
    }

}
